﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.Visionox_Gamma
{
    public partial class RM6729x_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        Gamma_Addr GAMMAs = new Gamma_Addr();

        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过

        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1; //lum
        private int flag2 = 1; //x
        private int flag3 = 1; //y
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        private byte RM19X_0xFE_XX = 0;
        private static bool Gamma_adjust_timeout_flag = false;
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        public int adjust_l = 0;
        public int adjust_x = 0;
        public int adjust_y = 0;
        //private int step_l = 1;
        //private int step_m = 5;
        //private int step_h = 12;

        private double R_x = 0.67;//
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;
        private double B_x = 0.14;
        private double B_y = 0.06;

        private ushort[] gamma_r = new ushort[33];
        private ushort[] gamma_g = new ushort[33];
        private ushort[] gamma_b = new ushort[33];

        //监听6729x挂载
        frmMain frmHelp = null;
        public RM6729x_Gamma(frmMain RM6729x)
        {
            frmHelp = RM6729x;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体       
        }  //publi
        //RM6729x 监听卸载
        private void RM6729x_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM6729x_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM671(2)9x_Gamma_FormClosed！\r\n";
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.Child_window_hide_already = false;
            frmHelp.IC_comboBox.Enabled = true;
        }
        private void RM6729x_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM6729x_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM671(2)9x_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(RM6729x_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM671(2)9x_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }


        //串口接收
        private void RM6729x_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x09: RM67195的Gamma读写、寄存器读写、OTP读写    设置  返回
                    // 0x19: RM67295的Gamma读写、寄存器读写、OTP读写    设置  返回
                    // 0x29: RM67198的Gamma读写、寄存器读写、OTP读写    设置  返回
                    // 0x2E: RM67198的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if ((buffer[0] == 0x09 || buffer[0] == 0x19 || buffer[0] == 0x29 || buffer[0] == 0x2E) && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        if ((Rec_tmp[0] == 0x09) || (Rec_tmp[0] == 0x19) || (Rec_tmp[0] == 0x29) || (Rec_tmp[0] == 0x2e))  //RM67195 & RM67295 & RM67198&RM69298
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写51h寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //写OTP烧录次数
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP次数\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_50 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_50 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录  
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_40 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_40 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_60 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_60 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_70 烧录\tOK！\r\n";//zangqiang 20171120
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_70 烧录\tNG！\r\n";//zangqiang 20171120
                                    }
                                    break;
                                case 0x07:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_90 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_90 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;

                                //zangqiang 20171120
                                case 0x17:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_A0 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_A0 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                //zangqiang 20171120

                                //zangqiang 20171121
                                case 0x18:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_D0 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_D0 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                case 0x19:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_E0 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_E0 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                //zangqiang 20171121

                                //zangqiang 20171125
                                case 0x1A:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_10 烧录\tOK！\r\n";//zangqiang 20171125
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_10 烧录\tNG！\r\n";//zangqiang 20171125
                                    }
                                    break;
                                case 0x1B:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_12 烧录\tOK！\r\n";//zangqiang 20171125
                                        }
                                        else
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP_Page_12 烧录\tNG！\r\n";//zangqiang 20171125
                                    }
                                    break;
                                //zangqiang 20171125


                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if (Rec_tmp[3] == 0x00)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if (Rec_tmp[3] == 0x42)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if (Rec_tmp[3] == 0x81)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x09:
                                    {
                                        MessageBox.Show("该功能已经删除！");
                                    }
                                    break;
                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        //Gamma保存
        private void RM6729x_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    if ((frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67295)) //rm67195
                    {
                        str = str + "R0," + NoRMR0.Text + "," + "G0," + NoRMG0.Text + "," + "B0," + NoRMB0.Text + "\r\n";//R
                        str = str + "R3," + NoRMR3.Text + "," + "G3," + NoRMG3.Text + "," + "B3," + NoRMB3.Text + "\r\n";
                        str = str + "R4," + NoRMR4.Text + "," + "G4," + NoRMG4.Text + "," + "B4," + NoRMB4.Text + "\r\n";
                        str = str + "R8," + NoRMR8.Text + "," + "G8," + NoRMG8.Text + "," + "B8," + NoRMB8.Text + "\r\n";
                        str = str + "R12," + NoRMR12.Text + "," + "G12," + NoRMG12.Text + "," + "B12," + NoRMB12.Text + "\r\n";
                        str = str + "R16," + NoRMR16.Text + "," + "G16," + NoRMG16.Text + "," + "B16," + NoRMB16.Text + "\r\n";
                        str = str + "R20," + NoRMR20.Text + "," + "G20," + NoRMG20.Text + "," + "B20," + NoRMB20.Text + "\r\n";
                        str = str + "R28," + NoRMR28.Text + "," + "G28," + NoRMG28.Text + "," + "B28," + NoRMB28.Text + "\r\n";
                        str = str + "R36," + NoRMR36.Text + "," + "G36," + NoRMG36.Text + "," + "B36," + NoRMB36.Text + "\r\n";
                        str = str + "R44," + NoRMR44.Text + "," + "G44," + NoRMG44.Text + "," + "B44," + NoRMB44.Text + "\r\n";
                        str = str + "R52," + NoRMR52.Text + "," + "G52," + NoRMG52.Text + "," + "B52," + NoRMB52.Text + "\r\n";
                        str = str + "R68," + NoRMR68.Text + "," + "G68," + NoRMG68.Text + "," + "B68," + NoRMB68.Text + "\r\n";//R
                        str = str + "R84," + NoRMR84.Text + "," + "G84," + NoRMG84.Text + "," + "B84," + NoRMB84.Text + "\r\n";
                        str = str + "R100," + NoRMR100.Text + "," + "G100," + NoRMG100.Text + "," + "B100," + NoRMB100.Text + "\r\n";
                        str = str + "R116," + NoRMR116.Text + "," + "G116," + NoRMG116.Text + "," + "B116," + NoRMB116.Text + "\r\n";
                        str = str + "R132," + NoRMR132.Text + "," + "G132," + NoRMG132.Text + "," + "B132," + NoRMB132.Text + "\r\n";
                        str = str + "R148," + NoRMR148.Text + "," + "G148," + NoRMG148.Text + "," + "B148," + NoRMB148.Text + "\r\n";
                        str = str + "R164," + NoRMR164.Text + "," + "G164," + NoRMG164.Text + "," + "B164," + NoRMB164.Text + "\r\n";
                        str = str + "R180," + NoRMR180.Text + "," + "G180," + NoRMG180.Text + "," + "B180," + NoRMB180.Text + "\r\n";
                        str = str + "R188," + NoRMR188.Text + "," + "G188," + NoRMG188.Text + "," + "B188," + NoRMB188.Text + "\r\n";
                        str = str + "R196," + NoRMR196.Text + "," + "G196," + NoRMG196.Text + "," + "B196," + NoRMB196.Text + "\r\n";
                        str = str + "R204," + NoRMR204.Text + "," + "G204," + NoRMG204.Text + "," + "B204," + NoRMB204.Text + "\r\n";
                        str = str + "R212," + NoRMR212.Text + "," + "G212," + NoRMG212.Text + "," + "B212," + NoRMB212.Text + "\r\n";//R
                        str = str + "R220," + NoRMR220.Text + "," + "G220," + NoRMG220.Text + "," + "B220," + NoRMB220.Text + "\r\n";
                        str = str + "R228," + NoRMR228.Text + "," + "G228," + NoRMG228.Text + "," + "B228," + NoRMB228.Text + "\r\n";
                        str = str + "R236," + NoRMR236.Text + "," + "G236," + NoRMG236.Text + "," + "B236," + NoRMB236.Text + "\r\n";
                        str = str + "R244," + NoRMR244.Text + "," + "G244," + NoRMG244.Text + "," + "B244," + NoRMB244.Text + "\r\n";
                        str = str + "R248," + NoRMR248.Text + "," + "G248," + NoRMG248.Text + "," + "B248," + NoRMB248.Text + "\r\n";
                        str = str + "R252," + NoRMR252.Text + "," + "G252," + NoRMG252.Text + "," + "B252," + NoRMB252.Text + "\r\n";
                        str = str + "R254," + NoRMR254.Text + "," + "G254," + NoRMG254.Text + "," + "B254," + NoRMB254.Text + "\r\n";
                        str = str + "R255," + NoRMR255.Text + "," + "G255," + NoRMG255.Text + "," + "B255," + NoRMB255.Text + "\r\n";
                    }
                    else if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198 || frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298) //rm67198
                    {
                        str = str + "R0," + NoRMR0.Text + "," + "G0," + NoRMG0.Text + "," + "B0," + NoRMB0.Text + "\r\n";//R
                        str = str + "R1," + NoRMR1.Text + "," + "G1," + NoRMG1.Text + "," + "B1," + NoRMB1.Text + "\r\n";
                        str = str + "R2," + NoRMR2.Text + "," + "G2," + NoRMG2.Text + "," + "B2," + NoRMB2.Text + "\r\n";
                        str = str + "R3," + NoRMR3.Text + "," + "G3," + NoRMG3.Text + "," + "B3," + NoRMB3.Text + "\r\n";
                        str = str + "R4," + NoRMR4.Text + "," + "G4," + NoRMG4.Text + "," + "B4," + NoRMB4.Text + "\r\n";
                        str = str + "R8," + NoRMR8.Text + "," + "G8," + NoRMG8.Text + "," + "B8," + NoRMB8.Text + "\r\n";
                        str = str + "R12," + NoRMR12.Text + "," + "G12," + NoRMG12.Text + "," + "B12," + NoRMB12.Text + "\r\n";
                        str = str + "R16," + NoRMR16.Text + "," + "G16," + NoRMG16.Text + "," + "B16," + NoRMB16.Text + "\r\n";
                        str = str + "R20," + NoRMR20.Text + "," + "G20," + NoRMG20.Text + "," + "B20," + NoRMB20.Text + "\r\n";
                        str = str + "R28," + NoRMR28.Text + "," + "G28," + NoRMG28.Text + "," + "B28," + NoRMB28.Text + "\r\n";
                        str = str + "R36," + NoRMR36.Text + "," + "G36," + NoRMG36.Text + "," + "B36," + NoRMB36.Text + "\r\n";
                        str = str + "R44," + NoRMR44.Text + "," + "G44," + NoRMG44.Text + "," + "B44," + NoRMB44.Text + "\r\n";
                        str = str + "R52," + NoRMR52.Text + "," + "G52," + NoRMG52.Text + "," + "B52," + NoRMB52.Text + "\r\n";
                        str = str + "R68," + NoRMR68.Text + "," + "G68," + NoRMG68.Text + "," + "B68," + NoRMB68.Text + "\r\n";//R
                        str = str + "R84," + NoRMR84.Text + "," + "G84," + NoRMG84.Text + "," + "B84," + NoRMB84.Text + "\r\n";
                        str = str + "R100," + NoRMR100.Text + "," + "G100," + NoRMG100.Text + "," + "B100," + NoRMB100.Text + "\r\n";
                        str = str + "R116," + NoRMR116.Text + "," + "G116," + NoRMG116.Text + "," + "B116," + NoRMB116.Text + "\r\n";
                        str = str + "R132," + NoRMR132.Text + "," + "G132," + NoRMG132.Text + "," + "B132," + NoRMB132.Text + "\r\n";
                        str = str + "R148," + NoRMR148.Text + "," + "G148," + NoRMG148.Text + "," + "B148," + NoRMB148.Text + "\r\n";
                        str = str + "R164," + NoRMR164.Text + "," + "G164," + NoRMG164.Text + "," + "B164," + NoRMB164.Text + "\r\n";
                        str = str + "R180," + NoRMR180.Text + "," + "G180," + NoRMG180.Text + "," + "B180," + NoRMB180.Text + "\r\n";
                        str = str + "R188," + NoRMR188.Text + "," + "G188," + NoRMG188.Text + "," + "B188," + NoRMB188.Text + "\r\n";
                        str = str + "R196," + NoRMR196.Text + "," + "G196," + NoRMG196.Text + "," + "B196," + NoRMB196.Text + "\r\n";
                        str = str + "R204," + NoRMR204.Text + "," + "G204," + NoRMG204.Text + "," + "B204," + NoRMB204.Text + "\r\n";
                        str = str + "R212," + NoRMR212.Text + "," + "G212," + NoRMG212.Text + "," + "B212," + NoRMB212.Text + "\r\n";//R
                        str = str + "R220," + NoRMR220.Text + "," + "G220," + NoRMG220.Text + "," + "B220," + NoRMB220.Text + "\r\n";
                        str = str + "R228," + NoRMR228.Text + "," + "G228," + NoRMG228.Text + "," + "B228," + NoRMB228.Text + "\r\n";
                        str = str + "R236," + NoRMR236.Text + "," + "G236," + NoRMG236.Text + "," + "B236," + NoRMB236.Text + "\r\n";
                        str = str + "R244," + NoRMR244.Text + "," + "G244," + NoRMG244.Text + "," + "B244," + NoRMB244.Text + "\r\n";
                        str = str + "R248," + NoRMR248.Text + "," + "G248," + NoRMG248.Text + "," + "B248," + NoRMB248.Text + "\r\n";
                        str = str + "R252," + NoRMR252.Text + "," + "G252," + NoRMG252.Text + "," + "B252," + NoRMB252.Text + "\r\n";
                        str = str + "R254," + NoRMR254.Text + "," + "G254," + NoRMG254.Text + "," + "B254," + NoRMB254.Text + "\r\n";
                        str = str + "R255," + NoRMR255.Text + "," + "G255," + NoRMG255.Text + "," + "B255," + NoRMB255.Text + "\r\n";
                    }

                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void RM6729x_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67295) //rm67295
                {
                    NoRMR0.Text = arrtmp[1].ToString(); NoRMG0.Text = arrtmp[3].ToString(); NoRMB0.Text = arrtmp[5].ToString();
                    NoRMR3.Text = arrtmp[7].ToString(); NoRMG3.Text = arrtmp[9].ToString(); NoRMB3.Text = arrtmp[11].ToString();
                    NoRMR4.Text = arrtmp[13].ToString(); NoRMG4.Text = arrtmp[15].ToString(); NoRMB4.Text = arrtmp[17].ToString();
                    NoRMR8.Text = arrtmp[19].ToString(); NoRMG8.Text = arrtmp[21].ToString(); NoRMB8.Text = arrtmp[23].ToString();
                    NoRMR12.Text = arrtmp[25].ToString(); NoRMG12.Text = arrtmp[27].ToString(); NoRMB12.Text = arrtmp[29].ToString();
                    NoRMR16.Text = arrtmp[31].ToString(); NoRMG16.Text = arrtmp[33].ToString(); NoRMB16.Text = arrtmp[35].ToString();
                    NoRMR20.Text = arrtmp[37].ToString(); NoRMG20.Text = arrtmp[39].ToString(); NoRMB20.Text = arrtmp[41].ToString();
                    NoRMR28.Text = arrtmp[43].ToString(); NoRMG28.Text = arrtmp[45].ToString(); NoRMB28.Text = arrtmp[47].ToString();
                    NoRMR36.Text = arrtmp[49].ToString(); NoRMG36.Text = arrtmp[51].ToString(); NoRMB36.Text = arrtmp[53].ToString();
                    NoRMR44.Text = arrtmp[55].ToString(); NoRMG44.Text = arrtmp[57].ToString(); NoRMB44.Text = arrtmp[59].ToString();
                    NoRMR52.Text = arrtmp[61].ToString(); NoRMG52.Text = arrtmp[63].ToString(); NoRMB52.Text = arrtmp[65].ToString();
                    NoRMR68.Text = arrtmp[67].ToString(); NoRMG68.Text = arrtmp[69].ToString(); NoRMB68.Text = arrtmp[71].ToString();
                    NoRMR84.Text = arrtmp[73].ToString(); NoRMG84.Text = arrtmp[75].ToString(); NoRMB84.Text = arrtmp[77].ToString();
                    NoRMR100.Text = arrtmp[79].ToString(); NoRMG100.Text = arrtmp[81].ToString(); NoRMB100.Text = arrtmp[83].ToString();
                    NoRMR116.Text = arrtmp[85].ToString(); NoRMG116.Text = arrtmp[87].ToString(); NoRMB116.Text = arrtmp[89].ToString();
                    NoRMR132.Text = arrtmp[91].ToString(); NoRMG132.Text = arrtmp[93].ToString(); NoRMB132.Text = arrtmp[95].ToString();
                    NoRMR148.Text = arrtmp[97].ToString(); NoRMG148.Text = arrtmp[99].ToString(); NoRMB148.Text = arrtmp[101].ToString();
                    NoRMR164.Text = arrtmp[103].ToString(); NoRMG164.Text = arrtmp[105].ToString(); NoRMB164.Text = arrtmp[107].ToString();
                    NoRMR180.Text = arrtmp[109].ToString(); NoRMG180.Text = arrtmp[111].ToString(); NoRMB180.Text = arrtmp[113].ToString();
                    NoRMR188.Text = arrtmp[115].ToString(); NoRMG188.Text = arrtmp[117].ToString(); NoRMB188.Text = arrtmp[119].ToString();
                    NoRMR196.Text = arrtmp[121].ToString(); NoRMG196.Text = arrtmp[123].ToString(); NoRMB196.Text = arrtmp[125].ToString();
                    NoRMR204.Text = arrtmp[127].ToString(); NoRMG204.Text = arrtmp[129].ToString(); NoRMB204.Text = arrtmp[131].ToString();
                    NoRMR212.Text = arrtmp[133].ToString(); NoRMG212.Text = arrtmp[135].ToString(); NoRMB212.Text = arrtmp[137].ToString();
                    NoRMR220.Text = arrtmp[139].ToString(); NoRMG220.Text = arrtmp[141].ToString(); NoRMB220.Text = arrtmp[143].ToString();
                    NoRMR228.Text = arrtmp[145].ToString(); NoRMG228.Text = arrtmp[147].ToString(); NoRMB228.Text = arrtmp[149].ToString();
                    NoRMR236.Text = arrtmp[151].ToString(); NoRMG236.Text = arrtmp[153].ToString(); NoRMB236.Text = arrtmp[155].ToString();
                    NoRMR244.Text = arrtmp[157].ToString(); NoRMG244.Text = arrtmp[159].ToString(); NoRMB244.Text = arrtmp[161].ToString();
                    NoRMR248.Text = arrtmp[163].ToString(); NoRMG248.Text = arrtmp[165].ToString(); NoRMB248.Text = arrtmp[167].ToString();
                    NoRMR252.Text = arrtmp[169].ToString(); NoRMG252.Text = arrtmp[171].ToString(); NoRMB252.Text = arrtmp[173].ToString();
                    NoRMR254.Text = arrtmp[175].ToString(); NoRMG254.Text = arrtmp[177].ToString(); NoRMB254.Text = arrtmp[179].ToString();
                    NoRMR255.Text = arrtmp[181].ToString(); NoRMG255.Text = arrtmp[183].ToString(); NoRMB255.Text = arrtmp[185].ToString();
                }
                else if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198 || frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298) //rm67198
                {
                    NoRMR0.Text = arrtmp[1].ToString(); NoRMG0.Text = arrtmp[3].ToString(); NoRMB0.Text = arrtmp[5].ToString();
                    NoRMR1.Text = arrtmp[7].ToString(); NoRMG1.Text = arrtmp[9].ToString(); NoRMB1.Text = arrtmp[11].ToString();
                    NoRMR2.Text = arrtmp[13].ToString(); NoRMG2.Text = arrtmp[15].ToString(); NoRMB2.Text = arrtmp[17].ToString();
                    NoRMR3.Text = arrtmp[19].ToString(); NoRMG3.Text = arrtmp[21].ToString(); NoRMB3.Text = arrtmp[23].ToString();
                    NoRMR4.Text = arrtmp[25].ToString(); NoRMG4.Text = arrtmp[27].ToString(); NoRMB4.Text = arrtmp[29].ToString();
                    NoRMR8.Text = arrtmp[31].ToString(); NoRMG8.Text = arrtmp[33].ToString(); NoRMB8.Text = arrtmp[35].ToString();
                    NoRMR12.Text = arrtmp[37].ToString(); NoRMG12.Text = arrtmp[39].ToString(); NoRMB12.Text = arrtmp[41].ToString();
                    NoRMR16.Text = arrtmp[43].ToString(); NoRMG16.Text = arrtmp[45].ToString(); NoRMB16.Text = arrtmp[47].ToString();
                    NoRMR20.Text = arrtmp[49].ToString(); NoRMG20.Text = arrtmp[51].ToString(); NoRMB20.Text = arrtmp[53].ToString();
                    NoRMR28.Text = arrtmp[55].ToString(); NoRMG28.Text = arrtmp[57].ToString(); NoRMB28.Text = arrtmp[59].ToString();
                    NoRMR36.Text = arrtmp[61].ToString(); NoRMG36.Text = arrtmp[63].ToString(); NoRMB36.Text = arrtmp[65].ToString();
                    NoRMR44.Text = arrtmp[67].ToString(); NoRMG44.Text = arrtmp[69].ToString(); NoRMB44.Text = arrtmp[71].ToString();
                    NoRMR52.Text = arrtmp[73].ToString(); NoRMG52.Text = arrtmp[75].ToString(); NoRMB52.Text = arrtmp[77].ToString();
                    NoRMR68.Text = arrtmp[79].ToString(); NoRMG68.Text = arrtmp[81].ToString(); NoRMB68.Text = arrtmp[83].ToString();
                    NoRMR84.Text = arrtmp[85].ToString(); NoRMG84.Text = arrtmp[87].ToString(); NoRMB84.Text = arrtmp[89].ToString();
                    NoRMR100.Text = arrtmp[91].ToString(); NoRMG100.Text = arrtmp[93].ToString(); NoRMB100.Text = arrtmp[95].ToString();
                    NoRMR116.Text = arrtmp[97].ToString(); NoRMG116.Text = arrtmp[99].ToString(); NoRMB116.Text = arrtmp[101].ToString();
                    NoRMR132.Text = arrtmp[103].ToString(); NoRMG132.Text = arrtmp[105].ToString(); NoRMB132.Text = arrtmp[107].ToString();
                    NoRMR148.Text = arrtmp[109].ToString(); NoRMG148.Text = arrtmp[111].ToString(); NoRMB148.Text = arrtmp[113].ToString();
                    NoRMR164.Text = arrtmp[115].ToString(); NoRMG164.Text = arrtmp[117].ToString(); NoRMB164.Text = arrtmp[119].ToString();
                    NoRMR180.Text = arrtmp[121].ToString(); NoRMG180.Text = arrtmp[123].ToString(); NoRMB180.Text = arrtmp[125].ToString();
                    NoRMR188.Text = arrtmp[127].ToString(); NoRMG188.Text = arrtmp[129].ToString(); NoRMB188.Text = arrtmp[131].ToString();
                    NoRMR196.Text = arrtmp[133].ToString(); NoRMG196.Text = arrtmp[135].ToString(); NoRMB196.Text = arrtmp[137].ToString();
                    NoRMR204.Text = arrtmp[139].ToString(); NoRMG204.Text = arrtmp[141].ToString(); NoRMB204.Text = arrtmp[143].ToString();
                    NoRMR212.Text = arrtmp[145].ToString(); NoRMG212.Text = arrtmp[147].ToString(); NoRMB212.Text = arrtmp[149].ToString();
                    NoRMR220.Text = arrtmp[151].ToString(); NoRMG220.Text = arrtmp[153].ToString(); NoRMB220.Text = arrtmp[155].ToString();
                    NoRMR228.Text = arrtmp[157].ToString(); NoRMG228.Text = arrtmp[159].ToString(); NoRMB228.Text = arrtmp[161].ToString();
                    NoRMR236.Text = arrtmp[163].ToString(); NoRMG236.Text = arrtmp[165].ToString(); NoRMB236.Text = arrtmp[167].ToString();
                    NoRMR244.Text = arrtmp[169].ToString(); NoRMG244.Text = arrtmp[171].ToString(); NoRMB244.Text = arrtmp[173].ToString();
                    NoRMR248.Text = arrtmp[175].ToString(); NoRMG248.Text = arrtmp[177].ToString(); NoRMB248.Text = arrtmp[179].ToString();
                    NoRMR252.Text = arrtmp[181].ToString(); NoRMG252.Text = arrtmp[183].ToString(); NoRMB252.Text = arrtmp[185].ToString();
                    NoRMR254.Text = arrtmp[187].ToString(); NoRMG254.Text = arrtmp[189].ToString(); NoRMB254.Text = arrtmp[191].ToString();
                    NoRMR255.Text = arrtmp[193].ToString(); NoRMG255.Text = arrtmp[195].ToString(); NoRMB255.Text = arrtmp[197].ToString();
                }
            }
        }



        //------------------------------------------RM67195_START-----------------------------------------

        private void RM67195_GAMMA_Click(object sender, EventArgs e)//67195的Gamma 读写
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[68];
            Setting.TempRecord.cmdLen = 68;
            Send_buf[2] = 63;
            Send_buf[3] = 0;

            if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67295)  //if RM67295
            {
                Send_buf[0] = 0x19;
            }
            else if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198)  //if RM67198
            {
                if (RM67198_rbt_33GP.Checked == true)
                {
                    Send_buf = new byte[72];
                    Setting.TempRecord.cmdLen = 72;
                    Send_buf[2] = 67;
                }
                else if (RM67198_rbt_19GP.Checked == true)
                {
                    Send_buf = new byte[44];
                    Setting.TempRecord.cmdLen = 44;
                    Send_buf[2] = 39;
                }
                //------------------------
                if (RM67198_rbt_NorM.Checked == true) //198 正常亮度gamma
                {
                    Send_buf[3] = 0;
                }
                else if (RM67198_rbt_HBM.Checked == true) //198 高亮gamma
                {
                    Send_buf[3] = 0x08;
                }
                Send_buf[0] = 0x29;
            }
            else if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298)  //if RM69298
            {
                if (RM67198_rbt_33GP.Checked == true)
                {
                    Send_buf = new byte[72];
                    Setting.TempRecord.cmdLen = 72;
                    Send_buf[2] = 67;
                }
                else if (RM67198_rbt_19GP.Checked == true)
                {
                    Send_buf = new byte[44];
                    Setting.TempRecord.cmdLen = 44;
                    Send_buf[2] = 39;
                }
                //------------------------
                if (RM67198_rbt_NorM.Checked == true) //198 正常亮度gamma
                {
                    Send_buf[3] = 0;
                }
                else if (RM67198_rbt_HBM.Checked == true) //198 高亮gamma
                {
                    Send_buf[3] = 0x08;
                }
                Send_buf[0] = 0x2e;
            }
            else
            {
                Send_buf[0] = 0x09; //RM67195
            }


            switch (btnTmp.Name)
            {
                default:
                    MessageBox.Show("该功能已经删除！");
                    break;
            }
            if (RM67198_rbt_33GP.Checked == true)//RM67198 33绑点
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[70], out Send_buf[71]);
            }
            else if (RM67198_rbt_19GP.Checked == true)//RM67198 19绑点
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[42], out Send_buf[43]);
            }
            else  //RM67195&RM67295&RM67198 31绑点
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[66], out Send_buf[67]);
            }
            try
            {
                RM19X_0xFE_XX = 0;
                if (RM67198_rbt_33GP.Checked == true)
                {
                    frmHelp.comPort.Write(Send_buf, 0, 72);   //RM67198 33绑点
                }
                else if (RM67198_rbt_19GP.Checked == true)
                {
                    frmHelp.comPort.Write(Send_buf, 0, 44);   //RM67198 19绑点
                }
                else
                {
                    frmHelp.comPort.Write(Send_buf, 0, 68);  //RM67195&RM67295&RM67198 31绑点
                }
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void RM67x95_trackBar_Scroll(object sender, EventArgs e)
        {
            RM67X95_label.Text = RM67x95_trackBar.Value.ToString() + "阶";
            if (RM19X_0xFE_XX != 0xF0)
            {
                RM19X_0xFE_XX = 0xF0;
                txtRM67195Add.Text = "fe";
                txtRM67195Data.Text = "00";
                RM196_Others_Click(RM67195_W_Reg51, e);
            }
            txtRM67195Add.Text = "51";
            txtRM67195Data.Text = RM67x95_trackBar.Value.ToString("x");
            RM196_Others_Click(RM67195_W_Reg51, e);
        }
        private void RM196_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67295)  //if RM67295
            {
                Send_buf[0] = 0x19;
            }
            else if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198)  //if RM67198
            {
                Send_buf[0] = 0x29;
            }
            else if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298)
            {
                Send_buf[0] = 0x2E;//rm69298
            }
            else
            {
                Send_buf[0] = 0x09; //RM67195
            }
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "RM67195_RegW":         //w写寄存器
                    {
                        Send_buf[1] = 0x01;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(txtRM67195Add.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(txtRM67195Data.Text.Trim(), 16)).ToString());
                        if ((Send_buf[3] == 0xfe) && (Send_buf[4] != 0))
                            RM19X_0xFE_XX = 0;
                    }
                    break;
                case "RM67195_W_Reg51":         //w写寄存器
                    {
                        Send_buf[1] = 0x51;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(txtRM67195Add.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(txtRM67195Data.Text.Trim(), 16)).ToString());
                    }
                    break;
                case "RM67195_1_OTPPage": //otp page1
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0A;
                        {       //保存待otp的gamma
                            FileStream fs = new FileStream("../OTP_gamma_195.txt", FileMode.OpenOrCreate, FileAccess.Write);
                            fs.Close();
                            StreamWriter sw = new StreamWriter(fs.Name, false, Encoding.UTF8);
                            string str = null;
                            str = str + NoRMR0.Text + "," + NoRMG0.Text + "," + NoRMB0.Text + "\r\n";
                            str = str + NoRMR3.Text + "," + NoRMG3.Text + "," + NoRMB3.Text + "\r\n";
                            str = str + NoRMR4.Text + "," + NoRMG4.Text + "," + NoRMB4.Text + "\r\n";
                            str = str + NoRMR8.Text + "," + NoRMG8.Text + "," + NoRMB8.Text + "\r\n";
                            str = str + NoRMR12.Text + "," + NoRMG12.Text + "," + NoRMB12.Text + "\r\n";
                            str = str + NoRMR16.Text + "," + NoRMG16.Text + "," + NoRMB16.Text + "\r\n";
                            str = str + NoRMR20.Text + "," + NoRMG20.Text + "," + NoRMB20.Text + "\r\n";
                            str = str + NoRMR28.Text + "," + NoRMG28.Text + "," + NoRMB28.Text + "\r\n";
                            str = str + NoRMR36.Text + "," + NoRMG36.Text + "," + NoRMB36.Text + "\r\n";
                            str = str + NoRMR44.Text + "," + NoRMG44.Text + "," + NoRMB44.Text + "\r\n";
                            str = str + NoRMR52.Text + "," + NoRMG52.Text + "," + NoRMB52.Text + "\r\n";
                            str = str + NoRMR68.Text + "," + NoRMG68.Text + "," + NoRMB68.Text + "\r\n";
                            str = str + NoRMR84.Text + "," + NoRMG84.Text + "," + NoRMB84.Text + "\r\n";
                            str = str + NoRMR100.Text + "," + NoRMG100.Text + "," + NoRMB100.Text + "\r\n";
                            str = str + NoRMR116.Text + "," + NoRMG116.Text + "," + NoRMB116.Text + "\r\n";
                            str = str + NoRMR132.Text + "," + NoRMG132.Text + "," + NoRMB132.Text + "\r\n";
                            str = str + NoRMR148.Text + "," + NoRMG148.Text + "," + NoRMB148.Text + "\r\n";
                            str = str + NoRMR164.Text + "," + NoRMG164.Text + "," + NoRMB164.Text + "\r\n";
                            str = str + NoRMR180.Text + "," + NoRMG180.Text + "," + NoRMB180.Text + "\r\n";
                            str = str + NoRMR188.Text + "," + NoRMG188.Text + "," + NoRMB188.Text + "\r\n";
                            str = str + NoRMR196.Text + "," + NoRMG196.Text + "," + NoRMB196.Text + "\r\n";
                            str = str + NoRMR204.Text + "," + NoRMG204.Text + "," + NoRMB204.Text + "\r\n";
                            str = str + NoRMR212.Text + "," + NoRMG212.Text + "," + NoRMB212.Text + "\r\n";
                            str = str + NoRMR220.Text + "," + NoRMG220.Text + "," + NoRMB220.Text + "\r\n";
                            str = str + NoRMR228.Text + "," + NoRMG228.Text + "," + NoRMB228.Text + "\r\n";
                            str = str + NoRMR236.Text + "," + NoRMG236.Text + "," + NoRMB236.Text + "\r\n";
                            str = str + NoRMR244.Text + "," + NoRMG244.Text + "," + NoRMB244.Text + "\r\n";
                            str = str + NoRMR248.Text + "," + NoRMG248.Text + "," + NoRMB248.Text + "\r\n";
                            str = str + NoRMR252.Text + "," + NoRMG252.Text + "," + NoRMB252.Text + "\r\n";
                            str = str + NoRMR254.Text + "," + NoRMG254.Text + "," + NoRMB254.Text + "\r\n";
                            str = str + NoRMR255.Text + "," + NoRMG255.Text + "," + NoRMB255.Text + "\r\n";
                            sw.WriteLine(str);
                            sw.Dispose(); //释放资源
                            sw.Close(); //关闭数据流
                        }
                    }
                    break;
                case "RM67195_2_OTPPage"://otp page0
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0D;
                    }
                    break;
                case "RM67195_3_OTPPage"://otp page2
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0E;
                    }
                    break;
                case "RM67195_4_OTPPage"://otp page3//zangqiang 20171120
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0f;
                    }
                    break;


                //zangqiang 20171120
                case "RM67195_CMD3_ANA_OTPPage"://CMD3_ANA_OTPPage
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x17;
                    }
                    break;
                //zangqiang 20171120

                //zangqiang 20171121
                case "RM67195_OTP_Page_D0"://OTP_Page_D0
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x18;
                    }
                    break;
                case "RM67195_OTP_Page_E0"://OTP_Page_E0
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x19;
                    }
                    break;
                //zangqiang 20171121

                //zangqiang 20171125
                case "RM67195_OTP_Page_10"://OTP_Page_10
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x1A;
                    }
                    break;
                case "RM67195_OTP_Page_12"://OTP_Page_12
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x1B;
                    }
                    break;
                //zangqiang 20171125



                case "RM67195_OTPPage_SPR"://otp page5//zangqiang 20171120
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x07;
                    }
                    break;
                case "RM67195_1_OTPtimeR":
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0B;
                        MessageBox.Show("该功能已经删除！");
                    }
                    break;
                case "RM67195_1_OTPtimeW":         //w写寄存器
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0C;
                        Send_buf[4] = byte.Parse((Convert.ToInt32(txtRM67195OTP.Text.Trim(), 16)).ToString());
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void RM67198_rbt_Gamma_Points()
        {
            if (RM67198_rbt_33GP.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_33GP, null);
            }
            if (RM67198_rbt_31GP.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_31GP, null);
            }
            if (RM67198_rbt_19GP.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_19GP, null);
            }
            if (RM67198_rbt_HBM.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_HBM, null);
            }
            if (RM67198_rbt_NorM.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_NorM, null);
            }
        }
        private void RM67198_rbt_Gamma_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton btnTmp = (RadioButton)sender;
            if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198)  //if RM67198
            {
                RM67198_gamma_point_cs.Text = "RM67198绑点切换";
            }
            else if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298)
            {
                RM67198_gamma_point_cs.Text = "RM69298绑点切换";
            }
            switch (btnTmp.Name)
            {
                case "RM67198_rbt_33GP":
                    {
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "DA"; txtRM67195Data.Text = "01";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        NoRMR1.Enabled = true; NoRMG1.Enabled = true; NoRMB1.Enabled = true;
                        NoRMR2.Enabled = true; NoRMG2.Enabled = true; NoRMB2.Enabled = true;
                        NoRMR3.Enabled = true; NoRMG3.Enabled = true; NoRMB3.Enabled = true;
                        NoRMR28.Enabled = true; NoRMG28.Enabled = true; NoRMB28.Enabled = true;
                        NoRMR44.Enabled = true; NoRMG44.Enabled = true; NoRMB44.Enabled = true;
                        NoRMR84.Enabled = true; NoRMG84.Enabled = true; NoRMB84.Enabled = true;
                        NoRMR116.Enabled = true; NoRMG116.Enabled = true; NoRMB116.Enabled = true;
                        NoRMR148.Enabled = true; NoRMG148.Enabled = true; NoRMB148.Enabled = true;
                        NoRMR180.Enabled = true; NoRMG180.Enabled = true; NoRMB180.Enabled = true;
                        NoRMR188.Enabled = true; NoRMG188.Enabled = true; NoRMB188.Enabled = true;
                        NoRMR204.Enabled = true; NoRMG204.Enabled = true; NoRMB204.Enabled = true;
                        NoRMR212.Enabled = true; NoRMG212.Enabled = true; NoRMB212.Enabled = true;
                        NoRMR220.Enabled = true; NoRMG220.Enabled = true; NoRMB220.Enabled = true;
                        NoRMR236.Enabled = true; NoRMG236.Enabled = true; NoRMB236.Enabled = true;
                        NoRMR248.Enabled = true; NoRMG248.Enabled = true; NoRMB248.Enabled = true;

                    }
                    break;
                case "RM67198_rbt_31GP":
                    {
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "DA"; txtRM67195Data.Text = "00";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        NoRMR1.Enabled = false; NoRMG1.Enabled = false; NoRMB1.Enabled = false;
                        NoRMR2.Enabled = false; NoRMG2.Enabled = false; NoRMB2.Enabled = false;
                        NoRMR3.Enabled = true; NoRMG3.Enabled = true; NoRMB3.Enabled = true;
                        NoRMR28.Enabled = true; NoRMG28.Enabled = true; NoRMB28.Enabled = true;
                        NoRMR44.Enabled = true; NoRMG44.Enabled = true; NoRMB44.Enabled = true;
                        NoRMR84.Enabled = true; NoRMG84.Enabled = true; NoRMB84.Enabled = true;
                        NoRMR116.Enabled = true; NoRMG116.Enabled = true; NoRMB116.Enabled = true;
                        NoRMR148.Enabled = true; NoRMG148.Enabled = true; NoRMB148.Enabled = true;
                        NoRMR180.Enabled = true; NoRMG180.Enabled = true; NoRMB180.Enabled = true;
                        NoRMR188.Enabled = true; NoRMG188.Enabled = true; NoRMB188.Enabled = true;
                        NoRMR204.Enabled = true; NoRMG204.Enabled = true; NoRMB204.Enabled = true;
                        NoRMR212.Enabled = true; NoRMG212.Enabled = true; NoRMB212.Enabled = true;
                        NoRMR220.Enabled = true; NoRMG220.Enabled = true; NoRMB220.Enabled = true;
                        NoRMR236.Enabled = true; NoRMG236.Enabled = true; NoRMB236.Enabled = true;
                        NoRMR248.Enabled = true; NoRMG248.Enabled = true; NoRMB248.Enabled = true;

                    }
                    break;
                case "RM67198_rbt_19GP":
                    {
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "DA"; txtRM67195Data.Text = "03";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        NoRMR1.Enabled = false; NoRMG1.Enabled = false; NoRMB1.Enabled = false;
                        NoRMR2.Enabled = true; NoRMG2.Enabled = true; NoRMB2.Enabled = true;
                        NoRMR3.Enabled = false; NoRMG3.Enabled = false; NoRMB3.Enabled = false;
                        NoRMR28.Enabled = false; NoRMG28.Enabled = false; NoRMB28.Enabled = false;
                        NoRMR44.Enabled = false; NoRMG44.Enabled = false; NoRMB44.Enabled = false;
                        NoRMR84.Enabled = false; NoRMG84.Enabled = false; NoRMB84.Enabled = false;
                        NoRMR116.Enabled = false; NoRMG116.Enabled = false; NoRMB116.Enabled = false;
                        NoRMR148.Enabled = false; NoRMG148.Enabled = false; NoRMB148.Enabled = false;
                        NoRMR180.Enabled = false; NoRMG180.Enabled = false; NoRMB180.Enabled = false;
                        NoRMR188.Enabled = false; NoRMG188.Enabled = false; NoRMB188.Enabled = false;
                        NoRMR204.Enabled = false; NoRMG204.Enabled = false; NoRMB204.Enabled = false;
                        NoRMR212.Enabled = false; NoRMG212.Enabled = false; NoRMB212.Enabled = false;
                        NoRMR220.Enabled = false; NoRMG220.Enabled = false; NoRMB220.Enabled = false;
                        NoRMR236.Enabled = false; NoRMG236.Enabled = false; NoRMB236.Enabled = false;
                        NoRMR248.Enabled = false; NoRMG248.Enabled = false; NoRMB248.Enabled = false;
                    }
                    break;
                case "RM67198_rbt_NorM":
                    {
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "E6"; txtRM67195Data.Text = "10";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "00";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "53"; txtRM67195Data.Text = "20";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                    }
                    break;
                case "RM67198_rbt_HBM":
                    {
                        if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198)  //if RM67198
                        {
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "E6"; txtRM67195Data.Text = "00";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "64"; txtRM67195Data.Text = "02";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "00";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "53"; txtRM67195Data.Text = "E0";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "51";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                        }
                        else if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298)  //if RM69298
                        {
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "E6"; txtRM67195Data.Text = "00";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "64"; txtRM67195Data.Text = "02";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "00";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "53"; txtRM67195Data.Text = "E0";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "52";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                        }
                    }
                    break;
                default: break;
            }
        }

        //RM67195
        private void btnOTPchk195_Click(object sender, EventArgs e)
        {

        }
        private int[] Parttern_RM67195 = new int[30] { 255, 254, 252, 248, 244, 236, 228, 220, 212, 204, 196, 188, 180, 164, 148, 132, 116, 100, 84, 68, 52, 44, 36, 28, 20, 16, 12, 8, 4, 3 };
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            text195_255.Text = (Math.Pow((Parttern_RM67195[0] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_254.Text = (Math.Pow((Parttern_RM67195[1] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_252.Text = (Math.Pow((Parttern_RM67195[2] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_248.Text = (Math.Pow((Parttern_RM67195[3] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_244.Text = (Math.Pow((Parttern_RM67195[4] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_236.Text = (Math.Pow((Parttern_RM67195[5] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_228.Text = (Math.Pow((Parttern_RM67195[6] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_220.Text = (Math.Pow((Parttern_RM67195[7] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_212.Text = (Math.Pow((Parttern_RM67195[8] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_204.Text = (Math.Pow((Parttern_RM67195[9] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_196.Text = (Math.Pow((Parttern_RM67195[10] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_188.Text = (Math.Pow((Parttern_RM67195[11] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_180.Text = (Math.Pow((Parttern_RM67195[12] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_164.Text = (Math.Pow((Parttern_RM67195[13] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_148.Text = (Math.Pow((Parttern_RM67195[14] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_132.Text = (Math.Pow((Parttern_RM67195[15] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_116.Text = (Math.Pow((Parttern_RM67195[16] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_100.Text = (Math.Pow((Parttern_RM67195[17] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_84.Text = (Math.Pow((Parttern_RM67195[18] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_68.Text = (Math.Pow((Parttern_RM67195[19] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_52.Text = (Math.Pow((Parttern_RM67195[20] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_44.Text = (Math.Pow((Parttern_RM67195[21] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_36.Text = (Math.Pow((Parttern_RM67195[22] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_28.Text = (Math.Pow((Parttern_RM67195[23] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_20.Text = (Math.Pow((Parttern_RM67195[24] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_16.Text = (Math.Pow((Parttern_RM67195[25] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_12.Text = (Math.Pow((Parttern_RM67195[26] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_8.Text = (Math.Pow((Parttern_RM67195[27] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_4.Text = (Math.Pow((Parttern_RM67195[28] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_3.Text = (Math.Pow((Parttern_RM67195[29] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
        }
        private void btnAutoGamma_Click(object sender, EventArgs e)//255阶为例
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[30, 3];
            D_lum = new double[30];
            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            //------------flexible gamma use------------------
            frmHelp.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            D_lum[0] = Convert.ToDouble(text195_255.Text);
            D_lum[1] = Convert.ToDouble(text195_254.Text);
            D_lum[2] = Convert.ToDouble(text195_252.Text);
            D_lum[3] = Convert.ToDouble(text195_248.Text);
            D_lum[4] = Convert.ToDouble(text195_244.Text);
            D_lum[5] = Convert.ToDouble(text195_236.Text);
            D_lum[6] = Convert.ToDouble(text195_228.Text);
            D_lum[7] = Convert.ToDouble(text195_220.Text);
            D_lum[8] = Convert.ToDouble(text195_212.Text);
            D_lum[9] = Convert.ToDouble(text195_204.Text);
            D_lum[10] = Convert.ToDouble(text195_196.Text);
            D_lum[11] = Convert.ToDouble(text195_188.Text);
            D_lum[12] = Convert.ToDouble(text195_180.Text);
            D_lum[13] = Convert.ToDouble(text195_164.Text);
            D_lum[14] = Convert.ToDouble(text195_148.Text);
            D_lum[15] = Convert.ToDouble(text195_132.Text);
            D_lum[16] = Convert.ToDouble(text195_116.Text);
            D_lum[17] = Convert.ToDouble(text195_100.Text);
            D_lum[18] = Convert.ToDouble(text195_84.Text);
            D_lum[19] = Convert.ToDouble(text195_68.Text);
            D_lum[20] = Convert.ToDouble(text195_52.Text);
            D_lum[21] = Convert.ToDouble(text195_44.Text);
            D_lum[22] = Convert.ToDouble(text195_36.Text);
            D_lum[23] = Convert.ToDouble(text195_28.Text);
            D_lum[24] = Convert.ToDouble(text195_20.Text);
            D_lum[25] = Convert.ToDouble(text195_16.Text);
            D_lum[26] = Convert.ToDouble(text195_12.Text);
            D_lum[27] = Convert.ToDouble(text195_8.Text);
            D_lum[28] = Convert.ToDouble(text195_4.Text);
            D_lum[29] = Convert.ToDouble(text195_3.Text);

            D_x = Convert.ToDouble(textx.Text);
            D_y = Convert.ToDouble(texty.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 30; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            //295 and 198
            {
                gamma_r = GAMMAs.RM295_Gamma_r;
                gamma_g = GAMMAs.RM295_Gamma_g;
                gamma_b = GAMMAs.RM295_Gamma_b;
            }

            txtRM67195Add.Text = "FE";
            if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198)
            {
                if (RM67198_rbt_HBM.Checked == true)
                {
                    txtRM67195Data.Text = "51";
                }
                else
                {
                    txtRM67195Data.Text = "50";
                }
            }
            else if (frmHelp.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298)
            {
                if (RM67198_rbt_HBM.Checked == true)
                {
                    txtRM67195Data.Text = "52";
                }
                else
                {
                    txtRM67195Data.Text = "50";
                }

            }
            else
                txtRM67195Data.Text = "05";
            RM196_Others_Click(RM67195_RegW, e);
            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(textlum_off.Text);
                        x_off = Convert.ToDouble(textx_off.Text);
                        y_off = Convert.ToDouble(texty_off.Text);
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnwhite, e);
                        T20.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }
        private void RM6729x_auto_gamma(Object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr = 0, lumg = 0, lumb = 0;
            frmHelp.Msr_CASDK();

            if ((frmHelp.CASDK_LUM < (D_lum[index] + lum_off)) && (frmHelp.CASDK_LUM > (D_lum[index] - lum_off)))//G
            {
                flag1 = 0;
            }
            if ((frmHelp.CASDK_y < (D_y + y_off)) && (frmHelp.CASDK_y > (D_y - y_off)) && flag1 == 0)//B
            {
                flag2 = 0;
            }
            if ((frmHelp.CASDK_x < (D_x + x_off)) && (frmHelp.CASDK_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
            {
                flag3 = 0;
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                if (index == 30)
                {
                    RM67195_GAMMA_Click(RM67195_GammaW_Red, null);
                    RM67195_GAMMA_Click(RM67195_GammaW_Green, null);
                    RM67195_GAMMA_Click(RM67195_GammaW_Blue, null);
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM67195/295/198 gamma调试完毕!\r\n";
                }
                if (index <= 29)
                {
                    switch (index)
                    {
                        case 0:
                            lum_off = (Convert.ToDouble(text195_255.Text) * Convert.ToDouble(text195_255_off.Text)) / 100;
                            break;
                        case 1:
                            lum_off = (Convert.ToDouble(text195_254.Text) * Convert.ToDouble(text195_254_off.Text)) / 100;
                            break;
                        case 2:
                            lum_off = (Convert.ToDouble(text195_252.Text) * Convert.ToDouble(text195_252_off.Text)) / 100;
                            break;
                        case 3:
                            lum_off = (Convert.ToDouble(text195_248.Text) * Convert.ToDouble(text195_248_off.Text)) / 100;
                            break;
                        case 4:
                            lum_off = (Convert.ToDouble(text195_244.Text) * Convert.ToDouble(text195_244_off.Text)) / 100;
                            break;
                        case 5:
                            lum_off = (Convert.ToDouble(text195_236.Text) * Convert.ToDouble(text195_236_off.Text)) / 100;
                            break;
                        case 6:
                            lum_off = (Convert.ToDouble(text195_228.Text) * Convert.ToDouble(text195_228_off.Text)) / 100;
                            break;
                        case 7:
                            lum_off = (Convert.ToDouble(text195_220.Text) * Convert.ToDouble(text195_220_off.Text)) / 100;
                            break;
                        case 8:
                            lum_off = (Convert.ToDouble(text195_212.Text) * Convert.ToDouble(text195_212_off.Text)) / 100;
                            break;
                        case 9:
                            lum_off = (Convert.ToDouble(text195_204.Text) * Convert.ToDouble(text195_204_off.Text)) / 100;
                            break;
                        case 10:
                            lum_off = (Convert.ToDouble(text195_196.Text) * Convert.ToDouble(text195_196_off.Text)) / 100;
                            break;
                        case 11:
                            lum_off = (Convert.ToDouble(text195_188.Text) * Convert.ToDouble(text195_188_off.Text)) / 100;
                            break;
                        case 12:
                            lum_off = (Convert.ToDouble(text195_180.Text) * Convert.ToDouble(text195_180_off.Text)) / 100;
                            break;
                        case 13:
                            lum_off = (Convert.ToDouble(text195_164.Text) * Convert.ToDouble(text195_164_off.Text)) / 100;
                            break;
                        case 14:
                            lum_off = (Convert.ToDouble(text195_148.Text) * Convert.ToDouble(text195_148_off.Text)) / 100;
                            break;
                        case 15:
                            lum_off = (Convert.ToDouble(text195_132.Text) * Convert.ToDouble(text195_132_off.Text)) / 100;
                            break;
                        case 16:
                            lum_off = (Convert.ToDouble(text195_116.Text) * Convert.ToDouble(text195_116_off.Text)) / 100;
                            break;
                        case 17:
                            lum_off = (Convert.ToDouble(text195_100.Text) * Convert.ToDouble(text195_100_off.Text)) / 100;
                            break;
                        case 18:
                            lum_off = (Convert.ToDouble(text195_84.Text) * Convert.ToDouble(text195_84_off.Text)) / 100;
                            break;
                        case 19:
                            lum_off = (Convert.ToDouble(text195_68.Text) * Convert.ToDouble(text195_68_off.Text)) / 100;
                            break;
                        case 20:
                            lum_off = (Convert.ToDouble(text195_52.Text) * Convert.ToDouble(text195_52_off.Text)) / 100;
                            break;
                        case 21:
                            lum_off = (Convert.ToDouble(text195_44.Text) * Convert.ToDouble(text195_44_off.Text)) / 100;
                            break;
                        case 22:
                            lum_off = (Convert.ToDouble(text195_36.Text) * Convert.ToDouble(text195_36_off.Text)) / 100;
                            break;
                        case 23:
                            lum_off = (Convert.ToDouble(text195_28.Text) * Convert.ToDouble(text195_28_off.Text)) / 100;
                            break;
                        case 24:
                            lum_off = (Convert.ToDouble(text195_20.Text) * Convert.ToDouble(text195_20_off.Text)) / 100;
                            break;
                        case 25:
                            lum_off = (Convert.ToDouble(text195_16.Text) * Convert.ToDouble(text195_16_off.Text)) / 100;
                            break;
                        case 26:
                            lum_off = (Convert.ToDouble(text195_12.Text) * Convert.ToDouble(text195_12_off.Text)) / 100;
                            break;
                        case 27:
                            lum_off = (Convert.ToDouble(text195_8.Text) * Convert.ToDouble(text195_8_off.Text)) / 100;
                            break;
                        case 28:
                            lum_off = (Convert.ToDouble(text195_4.Text) * Convert.ToDouble(text195_4_off.Text)) / 100;
                            break;
                        case 29:
                            lum_off = (Convert.ToDouble(text195_3.Text) * Convert.ToDouble(text195_3_off.Text)) / 100;
                            break;
                    }

                    frmHelp.numPatternR.Value = Parttern_RM67195[index];
                    frmHelp.numPatternG.Value = Parttern_RM67195[index];
                    frmHelp.numPatternB.Value = Parttern_RM67195[index];
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);//写入Pattern参数
                    Thread.Sleep(15); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r, g, b;
                int flexible_gamma_count = 100;

                Lr_t = CaSDKObj.R_y * ((frmHelp.CASDK_y - CaSDKObj.G_y) * (CaSDKObj.B_x - frmHelp.CASDK_x) + (frmHelp.CASDK_x - CaSDKObj.G_x) * (frmHelp.CASDK_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((frmHelp.CASDK_y - CaSDKObj.G_y) * (frmHelp.CASDK_x - CaSDKObj.R_x) + (frmHelp.CASDK_x - CaSDKObj.G_x) * (CaSDKObj.R_y - frmHelp.CASDK_y));
                Lb_t = 1.0;
                Lg_t = CaSDKObj.G_y * ((frmHelp.CASDK_y - CaSDKObj.R_y) * (CaSDKObj.B_x - frmHelp.CASDK_x) + (frmHelp.CASDK_x - CaSDKObj.R_x) * (frmHelp.CASDK_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((frmHelp.CASDK_y - CaSDKObj.R_y) * (frmHelp.CASDK_x - CaSDKObj.G_x) + (frmHelp.CASDK_x - CaSDKObj.R_x) * (CaSDKObj.G_y - frmHelp.CASDK_y));
                lumr = frmHelp.CASDK_LUM * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = frmHelp.CASDK_LUM * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = frmHelp.CASDK_LUM * Lb_t / (Lr_t + Lg_t + Lb_t);

                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR255.Value, NoRMG255.Value, NoRMB255.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR255.Value = r;
                                NoRMG255.Value = g;
                                NoRMB255.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30], Convert.ToUInt16(NoRMR255.Value), gamma_g[30], Convert.ToUInt16(NoRMG255.Value), gamma_b[30], Convert.ToUInt16(NoRMB255.Value));
                            }
                            break;
                        case 1:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR254.Value, NoRMG254.Value, NoRMB254.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR254.Value = r;
                                NoRMG254.Value = g;
                                NoRMB254.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[29], Convert.ToUInt16(NoRMR254.Value), gamma_g[29], Convert.ToUInt16(NoRMG254.Value), gamma_b[29], Convert.ToUInt16(NoRMB254.Value));

                            }
                            break;
                        case 2:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR252.Value, NoRMG252.Value, NoRMB252.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR252.Value = r;
                                NoRMG252.Value = g;
                                NoRMB252.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[28], Convert.ToUInt16(NoRMR252.Value), gamma_g[28], Convert.ToUInt16(NoRMG252.Value), gamma_b[28], Convert.ToUInt16(NoRMB252.Value));

                            }
                            break;
                        case 3:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR248.Value, NoRMG248.Value, NoRMB248.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR248.Value = r;
                                NoRMG248.Value = g;
                                NoRMB248.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[27], Convert.ToUInt16(NoRMR248.Value), gamma_g[27], Convert.ToUInt16(NoRMG248.Value), gamma_b[27], Convert.ToUInt16(NoRMB248.Value));

                            }
                            break;
                        case 4:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR244.Value, NoRMG244.Value, NoRMB244.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR244.Value = r;
                                NoRMG244.Value = g;
                                NoRMB244.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[26], Convert.ToUInt16(NoRMR244.Value), gamma_g[26], Convert.ToUInt16(NoRMG244.Value), gamma_b[26], Convert.ToUInt16(NoRMB244.Value));

                            }
                            break;
                        case 5:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR236.Value, NoRMG236.Value, NoRMB236.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR236.Value = r;
                                NoRMG236.Value = g;
                                NoRMB236.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[25], Convert.ToUInt16(NoRMR236.Value), gamma_g[25], Convert.ToUInt16(NoRMG236.Value), gamma_b[25], Convert.ToUInt16(NoRMB236.Value));


                            }
                            break;
                        case 6:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR228.Value, NoRMG228.Value, NoRMB228.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR228.Value = r;
                                NoRMG228.Value = g;
                                NoRMB228.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[24], Convert.ToUInt16(NoRMR228.Value), gamma_g[24], Convert.ToUInt16(NoRMG228.Value), gamma_b[24], Convert.ToUInt16(NoRMB228.Value));

                            }
                            break;
                        case 7:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR220.Value, NoRMG220.Value, NoRMB220.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR220.Value = r;
                                NoRMG220.Value = g;
                                NoRMB220.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[23], Convert.ToUInt16(NoRMR220.Value), gamma_g[23], Convert.ToUInt16(NoRMG220.Value), gamma_b[23], Convert.ToUInt16(NoRMB220.Value));

                            }
                            break;
                        case 8:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR212.Value, NoRMG212.Value, NoRMB212.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR212.Value = r;
                                NoRMG212.Value = g;
                                NoRMB212.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[22], Convert.ToUInt16(NoRMR212.Value), gamma_g[22], Convert.ToUInt16(NoRMG212.Value), gamma_b[22], Convert.ToUInt16(NoRMB212.Value));

                            }
                            break;
                        case 9:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR204.Value, NoRMG204.Value, NoRMB204.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR204.Value = r;
                                NoRMG204.Value = g;
                                NoRMB204.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[21], Convert.ToUInt16(NoRMR204.Value), gamma_g[21], Convert.ToUInt16(NoRMG204.Value), gamma_b[21], Convert.ToUInt16(NoRMB204.Value));

                            }
                            break;
                        case 10:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR196.Value, NoRMG196.Value, NoRMB196.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR196.Value = r;
                                NoRMG196.Value = g;
                                NoRMB196.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[20], Convert.ToUInt16(NoRMR196.Value), gamma_g[20], Convert.ToUInt16(NoRMG196.Value), gamma_b[20], Convert.ToUInt16(NoRMB196.Value));

                            }
                            break;
                        case 11:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR188.Value, NoRMG188.Value, NoRMB188.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR188.Value = r;
                                NoRMG188.Value = g;
                                NoRMB188.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[19], Convert.ToUInt16(NoRMR188.Value), gamma_g[19], Convert.ToUInt16(NoRMG188.Value), gamma_b[19], Convert.ToUInt16(NoRMB188.Value));

                            }
                            break;
                        case 12:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR180.Value, NoRMG180.Value, NoRMB180.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR180.Value = r;
                                NoRMG180.Value = g;
                                NoRMB180.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[18], Convert.ToUInt16(NoRMR180.Value), gamma_g[18], Convert.ToUInt16(NoRMG180.Value), gamma_b[18], Convert.ToUInt16(NoRMB180.Value));

                            }
                            break;
                        case 13:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR164.Value, NoRMG164.Value, NoRMB164.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR164.Value = r;
                                NoRMG164.Value = g;
                                NoRMB164.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[17], Convert.ToUInt16(NoRMR164.Value), gamma_g[17], Convert.ToUInt16(NoRMG164.Value), gamma_b[17], Convert.ToUInt16(NoRMB164.Value));

                            }
                            break;
                        case 14:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR148.Value, NoRMG148.Value, NoRMB148.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR148.Value = r;
                                NoRMG148.Value = g;
                                NoRMB148.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[16], Convert.ToUInt16(NoRMR148.Value), gamma_g[16], Convert.ToUInt16(NoRMG148.Value), gamma_b[16], Convert.ToUInt16(NoRMB148.Value));

                            }
                            break;
                        case 15:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR132.Value, NoRMG132.Value, NoRMB132.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR132.Value = r;
                                NoRMG132.Value = g;
                                NoRMB132.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[15], Convert.ToUInt16(NoRMR132.Value), gamma_g[15], Convert.ToUInt16(NoRMG132.Value), gamma_b[15], Convert.ToUInt16(NoRMB132.Value));

                            }
                            break;
                        case 16:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR116.Value, NoRMG116.Value, NoRMB116.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR116.Value = r;
                                NoRMG116.Value = g;
                                NoRMB116.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[14], Convert.ToUInt16(NoRMR116.Value), gamma_g[14], Convert.ToUInt16(NoRMG116.Value), gamma_b[14], Convert.ToUInt16(NoRMB116.Value));

                            }
                            break;
                        case 17:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR100.Value, NoRMG100.Value, NoRMB100.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR100.Value = r;
                                NoRMG100.Value = g;
                                NoRMB100.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[13], Convert.ToUInt16(NoRMR100.Value), gamma_g[13], Convert.ToUInt16(NoRMG100.Value), gamma_b[13], Convert.ToUInt16(NoRMB100.Value));

                            }
                            break;
                        case 18:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR84.Value, NoRMG84.Value, NoRMB84.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR84.Value = r;
                                NoRMG84.Value = g;
                                NoRMB84.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[12], Convert.ToUInt16(NoRMR84.Value), gamma_g[12], Convert.ToUInt16(NoRMG84.Value), gamma_b[12], Convert.ToUInt16(NoRMB84.Value));

                            }
                            break;
                        case 19:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR68.Value, NoRMG68.Value, NoRMB68.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR68.Value = r;
                                NoRMG68.Value = g;
                                NoRMB68.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[11], Convert.ToUInt16(NoRMR68.Value), gamma_g[11], Convert.ToUInt16(NoRMG68.Value), gamma_b[11], Convert.ToUInt16(NoRMB68.Value));

                            }
                            break;
                        case 20:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR52.Value, NoRMG52.Value, NoRMB52.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR52.Value = r;
                                NoRMG52.Value = g;
                                NoRMB52.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[10], Convert.ToUInt16(NoRMR52.Value), gamma_g[10], Convert.ToUInt16(NoRMG52.Value), gamma_b[10], Convert.ToUInt16(NoRMB52.Value));

                            }
                            break;
                        case 21:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR44.Value, NoRMG44.Value, NoRMB44.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR44.Value = r;
                                NoRMG44.Value = g;
                                NoRMB44.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[9], Convert.ToUInt16(NoRMR44.Value), gamma_g[9], Convert.ToUInt16(NoRMG44.Value), gamma_b[9], Convert.ToUInt16(NoRMB44.Value));

                            }
                            break;
                        case 22:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR36.Value, NoRMG36.Value, NoRMB36.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR36.Value = r;
                                NoRMG36.Value = g;
                                NoRMB36.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[8], Convert.ToUInt16(NoRMR36.Value), gamma_g[8], Convert.ToUInt16(NoRMG36.Value), gamma_b[8], Convert.ToUInt16(NoRMB36.Value));

                            }
                            break;
                        case 23:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR28.Value, NoRMG28.Value, NoRMB28.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR28.Value = r;
                                NoRMG28.Value = g;
                                NoRMB28.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[7], Convert.ToUInt16(NoRMR28.Value), gamma_g[7], Convert.ToUInt16(NoRMG28.Value), gamma_b[7], Convert.ToUInt16(NoRMB28.Value));

                            }
                            break;
                        case 24://20阶以下
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR20.Value, NoRMG20.Value, NoRMB20.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR20.Value = r;
                                NoRMG20.Value = g;
                                NoRMB20.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[6], Convert.ToUInt16(NoRMR20.Value), gamma_g[6], Convert.ToUInt16(NoRMG20.Value), gamma_b[6], Convert.ToUInt16(NoRMB20.Value));

                            }
                            break;
                        case 25:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR16.Value, NoRMG16.Value, NoRMB16.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR16.Value = r;
                                NoRMG16.Value = g;
                                NoRMB16.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[5], Convert.ToUInt16(NoRMR16.Value), gamma_g[5], Convert.ToUInt16(NoRMG16.Value), gamma_b[5], Convert.ToUInt16(NoRMB16.Value));

                            }
                            break;
                        case 26:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR12.Value, NoRMG12.Value, NoRMB12.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR12.Value = r;
                                NoRMG12.Value = g;
                                NoRMB12.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[4], Convert.ToUInt16(NoRMR12.Value), gamma_g[4], Convert.ToUInt16(NoRMG12.Value), gamma_b[4], Convert.ToUInt16(NoRMB12.Value));

                            }
                            break;
                        case 27:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR8.Value, NoRMG8.Value, NoRMB8.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR8.Value = r;
                                NoRMG8.Value = g;
                                NoRMB8.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[3], Convert.ToUInt16(NoRMR8.Value), gamma_g[3], Convert.ToUInt16(NoRMG8.Value), gamma_b[3], Convert.ToUInt16(NoRMB8.Value));

                            }
                            break;
                        case 28:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR4.Value, NoRMG4.Value, NoRMB4.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR4.Value = r;
                                NoRMG4.Value = g;
                                NoRMB4.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[2], Convert.ToUInt16(NoRMR4.Value), gamma_g[2], Convert.ToUInt16(NoRMG4.Value), gamma_b[2], Convert.ToUInt16(NoRMB4.Value));

                            }
                            break;
                        case 29:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NoRMR3.Value, NoRMG3.Value, NoRMB3.Value, Parttern_RM67195[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR3.Value = r;
                                NoRMG3.Value = g;
                                NoRMB3.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[1], Convert.ToUInt16(NoRMR3.Value), gamma_g[1], Convert.ToUInt16(NoRMG3.Value), GAMMAs.RM195_Gamma_b[1], Convert.ToUInt16(NoRMB3.Value));

                            }
                            break;
                    }
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("数值超限！");
                }
            }
        }
        private void btn195Stop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
            //--------reset---------------
            Gamma_adjust_timeout_flag = false;
            frmHelp.Gamma_auto_adjust_count = 0;
            //----------------------------
        }

        private void T20_Tick(object sender, EventArgs e)
        {
            RM6729x_auto_gamma(null, null);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "0e"; txtRM67195Data.Text = "80";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "20";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "b1"; txtRM67195Data.Text = "00";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "d0";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "55"; txtRM67195Data.Text = "30";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "07"; txtRM67195Data.Text = "01";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "0e"; txtRM67195Data.Text = "50";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "d0";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "42"; txtRM67195Data.Text = "81";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "27"; txtRM67195Data.Text = "55";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "FE"; txtRM67195Data.Text = "00";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
        }
        private void button7_Click(object sender, EventArgs e)
        {
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "0e"; txtRM67195Data.Text = "80";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "20";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "b1"; txtRM67195Data.Text = "00";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "d0";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "55"; txtRM67195Data.Text = "00";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "07"; txtRM67195Data.Text = "01";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "0e"; txtRM67195Data.Text = "50";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "d0";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "42"; txtRM67195Data.Text = "01";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "27"; txtRM67195Data.Text = "55";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
            txtRM67195Add.Text = "FE"; txtRM67195Data.Text = "00";
            RM196_Others_Click(RM67195_RegW, e);
            Thread.Sleep(15);
        }
        private void RM6729x_Gamma_Load(object sender, EventArgs e)
        {

        }
    }
}
